package com.tianhui.consignortwofei.mvp.ui.activity.hetong;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.UserInfoModel;
import com.tianhui.consignortwofei.mvp.model.enty.UserInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.LoginModel;
import com.tianhui.consignortwofei.mvp.ui.activity.SplashActivity;

import com.tianhui.consignortwofei.mvp.ui.activity.hetong.impl.ContractStatusHeTong;

public class HeTongActivityNew extends BaseActivity {

    private UserInfoModel mUserInfoModel;

    private LoginModel mLoginModel;

//    @Override
//    public String getToolbarSubtitle() {
//        return "退出登录";
//    }

//    @Override
//    public void onSubtitleClick() {
//        new MaterialDialog.Builder(getContext())
//                .content("确定要退出吗？")
//                .positiveText("是")
//                .negativeText("否")
//                .onPositive(new MaterialDialog.SingleButtonCallback() {
//                    @Override
//                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                        requestLogOut();
//                    }
//                })
//                .show();
//    }

//    private void requestLogOut() {
//
//
//        mLoginModel = new LoginModel();
//        mLoginModel.loginOutModel(getContext(), true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
//            @Override
//            public void onNext(CommonResponse commonResponse) {
//                if (commonResponse.getCode() == 20000) {
//                    SharedPreferenceManager.getInstance().setToken("");
//                    openActivity(LoginActivityNew.class);
//                    finish();
//                }
//
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                showToast(e.message);
//            }
//        });
//    }

//    @Override
//    public boolean isShowBackIcon() {
//        return false;
//    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_hetong_new;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    }

    @Override
    public void init() {

        setTitle("销售合同管理");
        requestUserInfo();
    }

    private void requestUserInfo() {
        mUserInfoModel = new UserInfoModel();

        mUserInfoModel.userInfoModel(getContext(), false, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<UserInfo>>() {

            @Override
            public void onNext(CommonResponse<UserInfo> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    UserInfo userInfo = commonResponse.getData();
                    if (userInfo != null && userInfo.profile != null) {
                        SharedPreferenceManager.getInstance().setConsignorId(userInfo.profile.id);
                        SharedPreferenceManager.getInstance().setConsignorName(userInfo.profile.name);
                        SharedPreferenceManager.getInstance().setPhone(userInfo.profile.phone);
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return new ContractListFragment().setOrderStatus(new ContractStatusHeTong());
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

//        int action = intent.getIntExtra(AppStatusConstant.KEY_HOME_ACTION, AppStatusConstant.ACTION_BACK_TO_HOME);
//        switch (action) {
//            case AppStatusConstant.ACTION_RESTART_APP:
//                restartApp();
//                break;
//        }
    }

    protected void restartApp() {
        openActivity(SplashActivity.class);
        finish();
    }

    private long exitTime = 0;

//    @Override
//    public void onBackPressed() {
//
//        if (System.currentTimeMillis() - exitTime > 2000) {
//            exitTime = System.currentTimeMillis();
//            showToast("再按一次退出程序");
//            return;
//        }
//        finish();
//    }

}
